package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.domain.db.SoulReport;
import com.tanhua.dubbo.api.SoulReportApi;
import com.tanhua.dubbo.mapper.SoulReportMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class SoulReportApiImpl implements SoulReportApi {

	@Autowired
	private SoulReportMapper soulReportMapper;

	/**
	 * 根据当前用户id查询所有问卷报告
	 */
	@Override
	public List<SoulReport> findReportListByUserId(Long userId) {
		QueryWrapper<SoulReport> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", userId);
		return soulReportMapper.selectList(wrapper);
	}

	/**
	 * 通过问卷id和用户id去查询是否有当前问卷的报告
	 */
	@Override
	public SoulReport findByUserIdAndPaperId(Long userId, Long paperId) {
		QueryWrapper<SoulReport> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", userId).eq("paper_id", paperId);
		return soulReportMapper.selectOne(wrapper);
	}

	/**
	 * 更新报告分数
	 */
	@Override
	public void update(SoulReport newSoulReport) {
		Wrapper<SoulReport> wrapper = new UpdateWrapper<>();
		soulReportMapper.update(newSoulReport,wrapper);
	}

	/**
	 * 保存问卷报告
	 */
	@Override
	public Long save(SoulReport newSoulReport) {
		soulReportMapper.insert(newSoulReport);

		return newSoulReport.getId();
	}

	/**
	 * 根据报告id查询报告
	 */
	@Override
	public SoulReport findById(Long reportId) {
		return soulReportMapper.selectById(reportId);
	}

	/**
	 * 根据分数范围查询相似的人
	 */
	@Override
	public List<SoulReport> findSimilarByScore(Long userId,Long minSimilarScore, Long maxSimilarScore) {
		QueryWrapper<SoulReport> wrapper = new QueryWrapper<>();
		wrapper.ne("user_id",userId);
		wrapper.ge("score",minSimilarScore).le("score",maxSimilarScore);
		return soulReportMapper.selectList(wrapper);
	}
}
